<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

if (!isset($_SESSION['superUser'])) {
	$msg = "";
	if (isset($_POST['submit_pwd'])) {
		if(md5($_POST['rootpwd']) != get_rootpwd() ) {
			sleep(5);
			$msg = '<center><br/><span style="color:red">';
			$msg .= _("Le mot de passe du superuser n'est pas valide, recommencez!");
			$msg .= "</span><br/>";
		}
		else
			$_SESSION['superUser'] = true;
	}
 	if (!isset($_SESSION['superUser'])) {
		echo $msg;
	?>
		<br/>
		<center>
			<form action=admin.php method=POST>
				<table>
					<tr>
						<td><?php echo _("Mot de passe superutilisateur"); ?> : </td>
						<td><input type="password" name="rootpwd" /></td>
					</tr>
				</table>
				<input type="submit" value="<?php echo _("Entrer"); ?>" name="submit_pwd"/>
				<input type="hidden" name="user" value="<?php echo $user; ?>"/>
				<input type="hidden" name="action" value="<?php echo $action; ?>"/>
				<input type="hidden" NAME="type" value="<?php echo $type; ?>"/>
			</form>
		</center>
	<?php
	}
}

if (!isset($_SESSION['superUser']))
	exit();

if (isset($_POST['sw']) && ($_POST['sw'] == "1")) {
    if ($_POST['rootpwd'] != $_POST['rootpwd2'] ) { 
		echo '<br/><center><span style="color:red">';
		echo _("Les deux mots de passe ne correspondent pas !");
		echo "</span><br/></center>";
    }
	else {		
		if (empty($_POST['rootpwd'])) 
			$pwd = get_rootpwd();
		else 
			$pwd = md5($_POST['rootpwd']);
		$dbConfig->set('title', $_POST['title']);
		$dbConfig->set('log_path', $_POST['logpath']);
		$dbConfig->set('pics_path', $_POST['picspath']);
		$dbConfig->set('covers_path', $_POST['coverspath']);
		$dbConfig->set('root_pwd', $pwd);
		$dbConfig->set('language', $_POST['language']);
		$dbConfig->set('max_record', $_POST['max_record']);
		$dbConfig->set('amazon_enable', $_POST['amazon_enable']);
		$dbConfig->set('amazon_AccessKeyId',  $_POST['amazon_accessKeyId']);
		$dbConfig->set('amazon_SecretAccessKey', $_POST['amazon_SecretKey']);
		$dbConfig->set('amazon_AssociateTag', $_POST['amazon_AssociateTag']);
		if ($dbConfig->write() === FALSE) { 
			echo _("PROBLEME : ") .biblio_db_error() ."<br>"; 
			exit;
		}  
		echo "<center><br/>";
		echo _("La configuration a &eacute;t&eacute; enregistr&eacute;e.");
		echo "</center>";
	}
}

$config_title       = get_title();          
$config_logpath     = get_logpath();      
$config_picspath    = get_picspath();      
$config_coverspath  = get_coverspath();      
$config_language    = get_language();
$config_max_record  = get_max_record();
$amazon_enable      = isAmazonEnabled();
$amazonAccessKeyId  = getAmazonKeyId();
$amazonSecretKey    = getAmazonSecret();
$amazonAssociateTag = getAmazonAssociateTag();
?>
<center>
	<h3><?php echo _("Configuration d'OpenBiblio"); ?></h3>
	<form action="admin.php" method="POST">
		<table border="0">
			<tr>
				<td><?php echo _("Titre"); ?> : </td>
				<td><input type="text" name="title" size="50" value="<?php echo $config_title; ?>"/></td>
			</tr>
			<tr>
				<td><?php echo _("R&eacute;pertoire des logs"); ?> : </td>
				<td><input type="text" name="logpath" size="50" value="<?php echo $config_logpath; ?>"/></td>
			</tr>
			<tr>
				<td><?php echo _("R&eacute;pertoire des photos"); ?> : </td>
				<td><input type="text" name="picspath" size="50" value="<?php echo $config_picspath; ?>"/></td>
			</tr>
			<tr>
				<td><?php echo _("R&eacute;pertoire des couverures"); ?> : </td>
				<td><input type="text" name="coverspath" size="50" value="<?php echo $config_coverspath; ?>"/></td>
			</tr>
			<tr>
				<td><?php echo _("Mote de passe du superutilisateur"); ?> : </td>
				<td><input type="password" name="rootpwd" size="10"/></td>
			</tr>
			<tr>
				<td><?php echo _("Mote de passe du superutilisateur (x2)"); ?> : </td>
				<td><input type="password" NAME="rootpwd2" size="10"/></td>
			</tr>
			<tr>
				<td><?php echo _("Langue"); ?> : </td>
				<td>
					<select name=language>
						<option value="fr_FR">fr_FR</option>
	<?php
	$handle = opendir('locale');
	while (false !== ($file = readdir($handle))) {
		// Ignore hidden files starting with .
		if (strpos($file, ".") === 0)
			continue;
		// Ignore non directories
		if (!is_dir('locale/'.$file))
			continue;
		if ($file == $config_language) 
			$str='selected="selected"';
		else 
			$str="";
		echo "<option value=$file $str>$file</option>";
	}
 	closedir($handle);
	?>
					</select>
				</td>
			</tr>
			<tr>
				<td><?php echo _("Nombre d'enregistrements<br>par page en mode recherche"); ?> : </td>
				<td><input type="text" name="max_record" size="3" value="<?php echo $config_max_record; ?>"/></td>
			</tr>
			<tr>
				<td><?php echo _("Activer la recherche sur Amazon"); ?> : </td>
				<td><input type="checkbox" name="amazon_enable" value="1" <?php if ($amazon_enable) echo 'checked="checked"'; ?> /></td>
			</tr>
			<tr>
				<td><?php echo _("Amazon Access Key Id"); ?> : </td>
				<td><input type="text" name="amazon_accessKeyId" size="50" value="<?php echo $amazonAccessKeyId; ?>"/></td>
			</tr>
			<tr>
				<td><?php echo _("Amazon Secret Key"); ?> : </td>
				<td><input type="text" name="amazon_SecretKey" size="50" value="<?php echo $amazonSecretKey; ?>"/></td>
			</tr>
			<tr>
				<td><?php echo _("Amazon Associate Tag"); ?> : </td>
				<td><input type="text" name="amazon_AssociateTag" size="50" value="<?php echo $amazonAssociateTag; ?>"/></td>
			</tr>			
        </table>
		<br/>
        <input type="hidden" name="user" value="<?php echo $user; ?>"/>
        <input type="hidden" name="action" value="<?php echo $action; ?>"/>
        <input type="hidden" name="type" value="<?php echo $type; ?>"/>
        <input type="hidden" name="sw" value="1"/>
        <input type="Submit" name="submit2" value="<?php echo _("Enregistrer"); ?>"/>
	</form>
</center>
<br/><br/>
<div>
	<span id=left-text>
		<a href="admin.php?menu=1"><?php echo _("retour menu administration"); ?></a>
		<br/>
		<a href="index.php"><?php echo _("retour menu principal"); ?></a>
	</span>
</div>
<br/>
